﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Diagnostics;
using System.Runtime.Serialization.Formatters.Binary;


namespace VRP_GTS
{
    class ProgramEntry
    {
        // a method can deep copy any types of serializable objects
        public static T DeepCopy<T>(T obj)
        {
            using (MemoryStream stream = new MemoryStream())
            {
                BinaryFormatter formatter = new BinaryFormatter();
                formatter.Serialize(stream, obj);
                stream.Position = 0;

                return (T)formatter.Deserialize(stream);
            }
        }

        static void Main(string[] args)
        {
            Stopwatch timer = new Stopwatch();                                                      // get running time
            timer.Start();                                                                          // start to calculate time               

            string inputFile;

            // If there are command line arguments, the first one is the file name
            if (args.Length != 0)
                inputFile = args[0];
            else
                inputFile = "Example1.vrp";

            // Create the parser object, and read in all the data from file. This object can read ANY TSPLIB file. Code borrowed from HeuristicLab.
            TSPLIBParser myParser = new TSPLIBParser(inputFile);
            myParser.Parse();

            // Create the vrp data object, using the parsed data
            CVRPData vrpData = new CVRPData(myParser);

            // Run the savings method heuristics
            CVRPSolution initSolution = Heuristics.SavingsMethod(vrpData);
            double initTotalCost = initSolution.TotalCost;
            int initNumOfVehicles = initSolution.TourList.Count;
            
            // Run the GTS heuristics
            CVRPSolution mySolution = Heuristics.GTS(vrpData, initSolution);

            timer.Stop();                                                                   // stop to calculate run time
            TimeSpan totalTime = timer.Elapsed;                                             // get the elapsed time as a TimeSpan value

            // Check if the solution is feasible
            mySolution.CheckFeasibility(vrpData);

            // report in a csv file 
            ////It is comma delimented, so if opening in a spreadsheet can seperate to different columns by commas
            string teamName = "1";
            string filepath = "1.csv";
            using (StreamWriter writer = new StreamWriter(filepath, true))
            {
                writer.WriteLine(teamName + "," + inputFile + "," + initTotalCost + "," + initNumOfVehicles + "," + mySolution.TotalCost + "," + mySolution.TourList.Count);
            }
            
        }

    }
}
